# -*- coding: utf-8 -*-
from scpy.logger import get_logger
import os
import sys
import requests
import time

reload(sys)
sys.setdefaultencoding('utf-8')

logger = get_logger(__file__)

CURRENT_PATH = os.path.dirname(__file__)
if CURRENT_PATH:
    CURRENT_PATH = CURRENT_PATH + "/"

def main():
    session = requests.session()
    params = {
        'id':209,
        'cookie_id':'',
        'from':'singlemessage',
        'isappinstalled':0
    }
    heads = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Cache-Control':'no-cache',
        'Connection':'keep-alive',
        'Cookie':'_site__site_upurl=NWE1ODRhNmI1OTEyYzk2MTExNzkwMzQ5OWUxZDA3YjcyMjBhNzE0NjdmMmYxZWViMGFhNWJmOWUzZDU1MDcxY79w6mgeuA4p%2FcYtBKecVJghs9tNwi2axSFJvo1FrzkMa6tP6cJ3dvPSb3hFahFfNSYdXOSNQ1WWB1JInjGtLdcgWmdiQo2GdyCmT33pG7YHV0IRAQrWJDyd2DJBmmXPPbCGlg0sdNyO7fqmedFMU8WYEFI9dyKc9eGoK98N%2BeTs; path=/',
        'Host':'www.zcxf.com',
        'Pragma':'no-cache',
        'Upgrade-Insecure-Requests':1,
        'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1 '
    }
    count = 1
    while True:
        try:
            response = session.get('http://www.zcxf.com/site/weixin/video_detail',params=params,headers=heads)
        except:
            print "retry get"
            time.sleep(3)
            print "sleeping 3 seconds for retry"
            continue
        if response.status_code == 200 and response.content !='':
            count += 1
            print "get success No.%d"%count
            print response.content
            time.sleep(3)
            print "sleeping for 3 seconds...."
            continue
        elif response.content =='':
            print "get filed No.%d"%count
            time.sleep(5)
            print "sleeping 5 seconds for retry"
        else:
            print "get filed No.%d"%count
            time.sleep(5)
            print "sleeping 5 seconds for retry"

def share():
    session = requests.session()
    heads = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Cache-Control':'no-cache',
        'Connection':'keep-alive',
        'Cookie':'_site__site_upurl=NWE1ODRhNmI1OTEyYzk2MTExNzkwMzQ5OWUxZDA3YjcyMjBhNzE0NjdmMmYxZWViMGFhNWJmOWUzZDU1MDcxY79w6mgeuA4p%2FcYtBKecVJghs9tNwi2axSFJvo1FrzkMa6tP6cJ3dvPSb3hFahFfNSYdXOSNQ1WWB1JInjGtLdcgWmdiQo2GdyCmT33pG7YHV0IRAQrWJDyd2DJBmmXPPbCGlg0sdNyO7fqmedFMU8WYEFI9dyKc9eGoK98N%2BeTs; path=/',
        'Host':'www.zcxf.com',
        'Pragma':'no-cache',
        'Upgrade-Insecure-Requests':1,
        'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1 '
    }
    count = 1
    while True:
        try:
            respose = session.post('http://www.zcxf.com/site/weixin/share',data={'id':209},headers=heads)
        except:
            print 'post filed, retry'
            time.sleep(3)
            print "sleeping 3 seconds for retry"
            continue
        print respose.content
        print "post success No.%d"%count
        time.sleep(3)
        print "sleeping for 3 seconds...."
        count += 1

def notice():
    heads = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Cache-Control':'no-cache',
        'Connection':'keep-alive',
        'Cookie':'_site__site_upurl=NWE1ODRhNmI1OTEyYzk2MTExNzkwMzQ5OWUxZDA3YjcyMjBhNzE0NjdmMmYxZWViMGFhNWJmOWUzZDU1MDcxY79w6mgeuA4p%2FcYtBKecVJghs9tNwi2axSFJvo1FrzkMa6tP6cJ3dvPSb3hFahFfNSYdXOSNQ1WWB1JInjGtLdcgWmdiQo2GdyCmT33pG7YHV0IRAQrWJDyd2DJBmmXPPbCGlg0sdNyO7fqmedFMU8WYEFI9dyKc9eGoK98N%2BeTs; path=/',
        'Host':'www.zcxf.com',
        'Pragma':'no-cache',
        'Upgrade-Insecure-Requests':1,
        'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1 '
    }
    count = 1
    while True:
        try:
            respose = requests.get('http://www.zcxf.com/site/weixin/thumb/',params={'id':209,'openid':44217},headers=heads)
        except:
            print 'get filed, retry'
            time.sleep(3)
            print "sleeping 10 seconds for retry"
            continue
        print respose.content
        print "post success No.%d"%count
        time.sleep(3)
        print "sleeping for 3 seconds...."
        count += 1

if __name__ == '__main__':
    # main()
    # share()
    # notice()
    if sys.argv[1]=='main':
        main()
    if sys.argv[1]=='share':
        share()
    if sys.argv[1]=='notice':
        notice()